from django.db import models
from django.urls import reverse
from datetime import datetime, timedelta

# Create your models here.


class Products(models.Model):
    name = models.CharField(max_length=100, blank=True,
                            null=False, verbose_name="产品名称")
    short_name = models.CharField(
        max_length=30, blank=True, null=True, verbose_name="产品简称")
    currency = models.CharField(
        max_length=10, blank=True, null=True, verbose_name="货币")
    # This field type is a guess.
    price = models.FloatField(blank=True, null=True, verbose_name="价格")
    brand = models.CharField(max_length=30, blank=True, null=True)
    seller = models.CharField(max_length=50, blank=True, null=True)
    reviews = models.IntegerField(blank=True, null=True)
    reviews_count = models.CharField(max_length=80, blank=True, null=True)
    # bought_last_month = models.CharField(max_length=30, blank=True, null=True)
    bought_last_month = models.IntegerField(blank=True, null=True)
    seller_rank = models.CharField(max_length=120, blank=True, null=True)
    image_path = models.CharField(max_length=80, blank=True, null=True)
    conn_tech = models.CharField(max_length=30, blank=True, null=True)
    connector_type = models.CharField(max_length=50, blank=True, null=True)
    ports = models.IntegerField(blank=True, null=True)
    compatiable_phones = models.CharField(
        max_length=100, blank=True, null=True)
    compatible_devices = models.CharField(
        max_length=100, blank=True, null=True)
    voltage = models.CharField(max_length=30, blank=True, null=True)
    wattage = models.CharField(max_length=30, blank=True, null=True)
    fast_charge = models.BooleanField(blank=True, null=True)
    # Field name made lowercase.
    asin = models.CharField(max_length=20, blank=True, null=True)
    url = models.CharField(max_length=300, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    features = models.CharField(max_length=500, blank=True, null=True)
    other_info = models.CharField(max_length=80, blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'products'
        verbose_name = '商品信息'
        verbose_name_plural = '商品信息'
        ordering = ['-create_time', 'reviews']

    def __str__(self):
        return f"{self.id}-{self.asin}-{self.short_name}"

    # 重写save方法
    def save(self, *args, **kwargs):
        # do_something()
        super().save(*args, **kwargs)
        # do_something_else()

    # 定义单个对象绝对路径
    def get_absolute_url(self):
        return reverse('datapanel:products_detail', kwargs={'pk': self.id})


TASK_TYPES = [
    ('ONCE', '一次性任务'),
    ('CRON', '定时任务')
]


class Tasks(models.Model):
    name = models.CharField(max_length=100, blank=True,
                            null=False, verbose_name="任务名称")
    kw = models.CharField(max_length=100, blank=True,
                          null=False, verbose_name="搜索词")
    type = models.CharField(max_length=4, choices=TASK_TYPES,
                            default="ONCE", verbose_name="任务类型")
    start_time = models.DateTimeField(
        blank=True, null=True,
        default=datetime.now()+timedelta(hours=2),
        verbose_name="开始时间")
    end_time = models.DateTimeField(blank=True, null=True, verbose_name="结束时间")
    cron_day = models.CharField(max_length=40, blank=True, null=True)
    cron_weekday = models.CharField(max_length=40, blank=True, null=True)
    cron_hour = models.CharField(max_length=40, blank=True, null=True)
    cron_minute = models.CharField(max_length=40, blank=True, null=True)
    tz = models.CharField(max_length=30, blank=True,
                          null=True, verbose_name="时区")
    status = models.SmallIntegerField(blank=True, null=True, verbose_name="状态")
    create_time = models.DateTimeField(
        blank=True, null=True,   verbose_name="创建时间")

    class Meta:
        db_table = 'tasks'
        managed = True
        verbose_name = '任务信息'
        verbose_name_plural = '任务信息'
        ordering = ['-create_time']

    def __str__(self):
        return f"{self.id}-{self.name}-{self.kw}"

    # 定义单个对象绝对路径
    def get_absolute_url(self):
        pass
        # return reverse('datapanel:products_details', kwargs={'pk': self.id})
